package com.zjitc.common;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/29 15:23
 * @description:
 */
public class ServerResponse<T> {

  private Integer status;
  private String msg;
  private T data;

  public ServerResponse() {
  }

  public ServerResponse(Integer status) {
    this.status = status;
  }

  public ServerResponse(Integer status, String msg) {
    this.status = status;
    this.msg = msg;
  }

  public ServerResponse(Integer status, T data) {
    this.status = status;
    this.data = data;
  }

  public boolean Success(){
    return this.status.equals(ServerResponseCode.SUCCESS);
  }

  public ServerResponse(Integer status, String msg, T data) {
    this.status = status;
    this.msg = msg;
    this.data = data;
  }

  public Integer getStatus() {
    return status;
  }

  public void setStatus(Integer status) {
    this.status = status;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public static<T> ServerResponse<T> createSuccess(Object...args){
    return create(ServerResponseCode.SUCCESS,args);
  }

  public static<T> ServerResponse<T> createError(Object...args){
    return create(ServerResponseCode.ERROR,args);
  }

  private static <T> ServerResponse<T> create(Integer status,Object...args){
    if(args.length>2){
      throw new IllegalArgumentException("argument is more than 2 :"+args.length);
    }
    if(args.length==1){
      if(args[0] instanceof String){
        return new ServerResponse(status, (String)args[0]);
      }else{
        return new ServerResponse<T>(status,(T)args[0]);
      }
    }
    if(args.length==2){
      return new ServerResponse(status,(String)args[0],(T)args[0]);
    }
    return new ServerResponse(status);
  }

}
